<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 物资装备管理
 * @package app\Equipe
 * @author LJQ<245369209@qq.com>
 */
class Equipe extends Api

{
	protected $wfType = 10;
    /**
   * 装备物资列表
   * @param string equipeId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function equipeList($scope = null){
		$where = [] ;
		if($scope){
			
			$where['enterpriseId'] = ['in',$scope];
		}
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//物资装备列表
			$info =collection(model('Equipe')->with('enterprise,store')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//物资装备列表
			$info =collection(model('Equipe')->with('enterprise,store')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	$count = model('Equipe')->where($where)->count('id'); 
    		if ($count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到物资','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'物资信息','count'=>$count,'data'=>$info]);
       		}
        return json(['code'=>0,'msg'=>'数据异常']);
   	 }
   	  
   	      //添加物资
   	 public function add($data){
          
          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Basic/Equipe');
          $result = $model->change($data);
          if (false != $result) {
            setlog('Equipeadd','Equipe',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'物资添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'物资添加失败,请检查']);
          }
      }

   	

   /**
   * 修改物资
   * @param  string equipeId 物资id,
   * @return Json
   */
   	public function edit($data,$equipeId){
        
          if (empty($equipeId)) {
              return json(['code'=>0,'msg'=>'物资不存在,请检查']);
          }
          $model = model('Basic/Equipe');     
          if ($model->change($data)) {
            setlog('Equipeupdate','Equipe',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'物资信息修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'物资信息修改失败,请检查']);
            }
    }
    /**
   *  删除物资
   * @param string equipeId 物资id,
   * @param Json
   */
     
    public function del($equipeId) {
      	  $equipeId = input('equipeId',0,'intval');
             $model = model('Basic/Equipe');
      	if (empty ( $equipeId )||empty(($model->where(array('id'=>$equipeId))->find()))) {
           	return  json(['code'=>0,'msg'=>'物资id不存在,请检查']);
        }
        if($model->destroy(['id'=>$equipeId]) !== FALSE){
           setlog('Equipedel','Equipe',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'物资删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'物资删除失败,请检查']);
       }
    }

     
   /**
   *   查询物资
   * @param string enterpriseId 企业id
   * @param sting material 物资名称
   * @param typeId 类型Id
   * @return Json
   */
   	public function search($material = null,$enterpriseId=0,$typeId = null){
   		$where = [] ;

		if($enterpriseId!=0){
			
			$where['enterpriseId'] = ['eq',$enterpriseId];
		}

		if($material){
			$where['material'] = ['like','%'.$material.'%'];
		}

        if($typeId){
            $where['typeId'] = ['eq',$typeId];
        }
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//物资装备列表
			$info =collection(model('Equipe')->with('enterprise,store')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//物资装备列表
			$info =collection(model('Equipe')->with('enterprise,store')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('Equipe')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到物资','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'物资信息','count'=>$count,'data'=>$info]);
       	}
      }

         /**
   *   获得企业列表
   * @return Json
   */

      public function getEnterprise() {
           $enterprise = model('basic/Enterprise');
           $enterprsies = collection($enterprise->field('id,fullName')->order('id')->select())->toArray();
           $enterprsies[] = array_unshift($enterprsies, ['id'=>0 , 'fullName'=>'管委会']);
           return json(['code'=>1,'msg'=>'企业列表','data'=>$enterprsies]);
    }


    /**
   *   获得员工列表
   * @param string enterpriseId 企业id.
   * @return Json
   */

      public function getStaff($enterpriseId=null) {
           if (empty($enterpriseId)) {
           return json(['code'=>0,'msg'=>'未选择企业']);
           }
           $staff = model('basic/Staff');
           $staffs = collection($staff->field('id,staffName')->order('id')->where(array('enterpriseId'=>$enterpriseId))->select())->toArray();
           return json(['code'=>1,'msg'=>'员工列表','data'=>$staffs]);
    }
    
    /**
     * 申请审核
     * @param string $hazardId
     */
    public function audit($equipeId = null){
    	$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
    	if(empty($workflow)||empty($workflow->steps)){
    		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
    	}else{
    	$equipe = model('Equipe')->get($equipeId);
	    	if($equipe->isUpdate(true)->save(['checkStatus'=>2]) !== false){
	    		$data['title'] = $equipe['material'];
	    		$data['wfFlowInfoId'] = $workflow['id'];
	    		$data['wfType'] = $this->wfType;
	    		$data['busiId'] = $equipeId;
	    		$data['enterpriseId'] = $equipe['enterpriseId'];
	    		$data['applyPerson'] = session('user_auth.id');
	    		if(model('Wfitem')->save($data)){
	    			return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
	    		}else{
	    			return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
	    		}
	    	}
    	}
    	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
    }
}